/*
 * Created on Feb 5, 2004
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.vectrics.skill.struts;

import org.vectrics.DataException;
import org.apache.log4j.Logger;
import org.vectrics.user.Person;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.skill.SkillProvider;
import org.vectrics.skill.UserEvaluation;
import org.vectrics.struts.VectricsTagSupport;

import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.io.IOException;




public class UserEvaluationTag extends VectricsTagSupport {
	private String evaluationId = null;
	
	private transient static Logger log = Logger.getLogger(UserEvaluationTag.class);
	private int bgClass = 0;
	
	public String makeStartTag()  {
		String html = "";
		if (evaluationId == null) {
			log.error("evaluationId was not set before rendering");
			return "evaluationId was not set before rendering";
		}
		try {
			UserEvaluation eval = RecruitServiceLocator.getSkillProvider().findEvaluationById(new Long(this.evaluationId));
			Table table = new Table();
			table.setWidth(450);
			table.setCellSpacing(4);
			table.setCellPadding(2);
			Person user = eval.getPerson();
			makeFieldRow(table, "Person", user.getName());
			makeFieldRow(table, "Description", eval.getDescription());
			makeFieldRow(table, "Notes", eval.getNotes());
			makeFieldRow(table, this.getPhrase("prompt.evaluation.score"), eval.getScore().toString());
			makeFieldRow(table, this.getPhrase("prompt.evaluation.type"), eval.getType().getDescription());
			String evaluatorName = "";
			Person evaluator = eval.getLeadPerson();
			if (evaluator != null) {
				evaluatorName = evaluator.getName();
			}
			makeFieldRow(table, "Evaluator", evaluatorName);
			
			
			html = table.toString();			
		}
		catch (DataException se) {
			log.error("DataException finding evaluation", se);
			html = "DataException finding evaluation";
			html += se.getMessage()+ "<br />";
			html += se.toString();
		}
		return html;
		
	}
		
	
	protected Row makeFieldRow(Table table, String caption, String value) {
		if (bgClass > 1)
			bgClass = 0;
		
		String cellClass ="formBg1";
		if (bgClass > 0)
			cellClass = "formBg2";
		
		log.debug("makeFieldRow() - creating display field in row of html table");
		if ((value == null) || (value.trim().equals(""))) {
			value = "<i>Not Given</i>";
		}
		
		Row row = table.addRow();
		Cell cell = row.addCell();
		cell.setVAlign("top");
		cell.setClass(cellClass);
		cell.addElement(TextHelper.getFont("stdFont", "<b>" + caption + "</b>"));
		cell = row.addCell();
		cell.setClass(cellClass);
		cell.addElement(TextHelper.getFont("stdFont", value));
		bgClass++;
		return(row);
	}
	

	/**
	 * @return Returns the id.
	 */
	public String getEvaluationId() {
		return evaluationId;
	}

	/**
	 * @param id The id to set.
	 */
	public void setEvaluationId(String id) {
		this.evaluationId = id;
	}



}
